package com.aepryus.loom;

import java.sql.*;
import java.io.*;

public class PersistenceException extends SQLException {
	private String sql;
	private String root;

	public PersistenceException (SQLException e) {
		this(e,"");
	}
	public PersistenceException (SQLException e,String sql) {
		super(e.getMessage(),e.getSQLState(),e.getErrorCode());
		this.sql = sql;
		Writer write = new StringWriter();
		PrintWriter pw = new PrintWriter(write);
		e.printStackTrace(pw);
		pw.close();
		try {
			write.close();
		} catch (IOException ioe) {/**/}
		root = write.toString();
	}
	public PersistenceException (String message) {
		super(message);
	}

	public String getSQL () {return sql;}

// SQLException ================================================================
	public String toString () {
		StringBuffer sb = new StringBuffer();

		sb.append("[");
		sb.append(getErrorCode());
		sb.append("][");
		sb.append(getSQLState());
		sb.append("][");
		sb.append(getMessage());
		sb.append("][");
		sb.append(sql);
		sb.append("]\n");
		sb.append("ROOT CAUSE:");
		sb.append(root);

		return sb.toString();
	}
}

